<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>
    <style>
        .layui-form-label {
            width: 130px;
        }
        .layui-input-block {
            margin-left: 130px;
        }
    </style>

</head>
<body>
<form class="ares-p layui-form" lay-filter="form">
    <div class="layui-form-item">
        <label class="layui-form-label">治疗单类别</label>
        <div class="layui-input-block">
            <select id="type" name="typeId" lay-verify="required">
                <option value=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">项目名称</label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入项目名称" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">编码(选填)</label>
        <div class="layui-input-block">
            <input name="code" placeholder="请输入编码" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否使用设备</label>
        <div class="layui-input-block">
            <select name="useDevice" lay-verify="required">
                <option value="true">是</option>
                <option value="false">否</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">治疗时间(分钟)</label>
        <div class="layui-input-block">
            <select name="cureTime" lay-verify="required">
                <option value="5">5</option>
                <option value="10">10</option>
                <option value="15">15</option>
                <option value="20">20</option>
                <option value="25">25</option>
                <option value="30">30</option>
                <option value="35">35</option>
                <option value="40">40</option>
                <option value="45">45</option>
                <option value="50">50</option>
                <option value="55">55</option>
                <option value="60">60</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格(元)</label>
        <div class="layui-input-block">
            <input name="price" type="number" placeholder="请输入价格" class="layui-input" lay-verify="required|number">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submit">提交表单</button>
        </div>
    </div>
</form>

</body>

<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['form', 'utils', 'mandyServices', 'qs'], function (form, utils, mandyServices, qs) {
        var queryParams = qs.parse(location.search.substr(1)),
            $type = $('#type');


        form.on('submit(submit)', function (data) {
            var fields = data.field,
                service = null,
                $this = $(data.elem);
            console.log(fields,$this)
            if(queryParams.id){
                service = modifyProjectMes
            }else {
                service = addProject
            }
            if(!service) return;
            $this.prop('disabled',true).addClass('layui-btn-disabled')
            service(fields).then(function (res) {
                utils.msgSuccess(res.message)
                $this.prop('disabled',false).removeClass('layui-btn-disabled')
                utils.ARES.event.emit('updateProjectList');
                utils.layer.closeAll('iframe');
            }).catch(function (err) {
                $this.prop('disabled',false).removeClass('layui-btn-disabled')
            })
            return false;
        });

        form.render();

        if(queryParams.id){
            getTypes().then(function () {
                buildProjectForm()
            })
        }else {
            getTypes();
        }


        //获取处方类别
        function getTypes() {
            return mandyServices.mandy_get_prescriptionType_syn().then(function (res) {
                var prcpType = $('select[name="typeId"]'),
                    data = res.data;
                prcpType.empty();
                $.each(data.prcp_type_list, function (idx, val) {
                    var option = $("<option>");
                    option.text(val.name);
                    option.attr("value", val.id);
                    prcpType.append(option);
                })
                form.render('select');
            })
        }

        //增加项目
        function addProject(data) {
           return mandyServices.mandy_post_add_subitem(data.typeId, data.name, data.code, data.price, data.useDevice, data.cureTime);
        }

        //查询单条项目信息(修改项目信息)
        function buildProjectForm() {
            mandyServices.mandy_get_subitem_by_id(queryParams.id).then( function (res) {
                var data = res.data;
                $type.data("type", data.type).prop('disabled',true);
                form.val("form", {
                    typeId: data.typeId,
                    name: data.itemName,
                    code: data.itemCode,
                    useDevice: data.isUse.toString(),
                    cureTime: data.treatTime,
                    price: data.unitFee,
                });
            })

        }

        //修改项目信息
        function modifyProjectMes(data) {
            return mandyServices.mandy_update_subitem(queryParams.id, data.typeId, $type.data("type"), data.name, data.code, data.useDevice, data.price, data.cureTime);
        }

    })
</script>


</html>